package com.miage.m1.javabrowser;

// Label perso servant a personnaliser les icones et le nom des fichier dans le panneau principal
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class MyLabel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String text;
	String pathName;

	public MyLabel(String text, String pathName) {
		this.text = text;
		this.pathName = pathName;
		this.setPreferredSize(new Dimension(22, 100));
		this.setVisible(true);
	}

	public void paintComponent(Graphics g) {

		super.paintComponent(g);
		g.drawString(text, 20, 20);
		ImageIcon i = getIcon(this.getExtension());

		i.paintIcon(this, g, 0, 0);
	}

	public ImageIcon getIcon(int indice) {

		String[] ic = { "txt.png", "img.png", "img.png", "dir.png",
				"blank.png", "blank.png" };

		return new ImageIcon("Icones\\" + ic[indice]);

	}

	public int getExtension() {
		int i;
		String[] ext = { "txt", "png", "jpg", "dir", "class" };
		int beg = text.lastIndexOf(".");

		String exts = (new File(this.pathName).isDirectory()) ? "dir" : (beg >0) ?text.substring(beg + 1):"";

		System.out.println("[" + exts + "]");

		for (i = 0; i < ext.length; i++)
			if (ext[i].equals(exts)) {
				break;
			}

		return i;

	}

	public String getText() {

		return this.text;
	}

	public String getPathName() {

		return this.pathName;
	}

}
